import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/sub/ImageViewPage.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../../util/DateUtil.dart';
import '../../../../util/ImConstant.dart';
import '../../../../util/ImageUtil.dart';
import '../../../../util/MessageUtil.dart';
import '../../../bloc/ImagePageCubit.dart';
import '../../../sub/ImCollectInfoPage.dart';
import '../pane/MessagePane.dart';

class CollectItemView extends StatefulWidget {
  final ImCollect collect;

  const CollectItemView({required this.collect, super.key});

  @override
  State<StatefulWidget> createState() => CollectItemState();
}

class CollectItemState extends State<CollectItemView> {
  @override
  Widget build(BuildContext context) {
    Size scrSize = MediaQuery.of(context).size;
    double screenWidth = scrSize.width * 0.85;
    return InkWell(
      onTap: () {
        showCollectInfo();
      },
      child: Padding(
        padding: const EdgeInsets.only(top: 5, bottom: 5, left: 10, right: 10),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            MessageRefPane(
                message: widget.collect.message!, param: MessageRefParam(maxLines: 100, refMaxLength: screenWidth)),
            const SizedBox(
              height: 5,
            ),
            _buildUserInfo()
          ],
        ),
      ),
    );
  }

  Widget _buildUserInfo() {
    return Padding(
      padding: const EdgeInsets.only(left: 3),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(widget.collect.message!.fromImUser?.userName ?? '',
              style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE)),
          Text(
            '${DateUtil.sessionShowTime(widget.collect.updateTime)}',
            style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE),
          ),
        ],
      ),
    );
  }

  void showCollectInfo() async {
    var collect = widget.collect;
    if (collect.messageType == ImMessageType.Image) {
      var imagePath = MessageUtil.getFilePath2(collect.message!);
      List<int> sizes = await ImageUtil.getImageSize(imagePath);
      Navigator.of(context).push(MaterialPageRoute(
          builder: (context) {
            return BlocProvider(
              create: (context) => ImagePageCubit(),
              child: ImageViewPage(
                  imageWidth: sizes[0],
                  imageHeight: sizes[1],
                  imagePath: imagePath,
                  messageId: collect.message?.id ?? (collect.id ?? 0)),
            );
          },
          settings: const RouteSettings(name: '/CollectInfoPage')));
      return;
    }
    Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return ImCollectInfoPage(
            collect: widget.collect,
          );
        },
        settings: const RouteSettings(name: '/CollectInfoPage')));
  }
}
